package com.kgdev.cweb.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.kgdev.cweb.beans.pub.impl.ChocolateColorBodyBean;
import com.kgdev.cweb.entities.pub.ChocolateColorBody;

@FacesConverter(forClass = ChocolateColorBody.class)
public class ChocolateColorBodyConverter implements Converter {

	private static ChocolateColorBodyBean chocolateColorBobyObjBean;

	{
		try {
			InitialContext ic = new InitialContext();
			chocolateColorBobyObjBean = (ChocolateColorBodyBean) ic
					.lookup("java:global/ChocolorEAR/ChocolorEJB/ChocolateColorBodyBean!com.kgdev.cweb.beans.pub.impl.ChocolateColorBodyBean");
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		String chocolateColor = value;
		ChocolateColorBody chocolateColorBobyObj = chocolateColorBobyObjBean.find(chocolateColor);
		return chocolateColorBobyObj;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		ChocolateColorBody chocolateColorBobyObj = (ChocolateColorBody) value;
		return chocolateColorBobyObj.getChocolateColor();
	}

}
